﻿@model pbvs.Models.ViewModels.DefaultViewModel
@{
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}  
<script type="text/javascript" src="@Url.Content("~/Scripts/linq.js")"></script>
<h1>Site Defaults</h1>
@Html.Hidden("model", Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model)), new { @id = "model" })
<ul>
	@foreach (var item in Model.Defaults)
	{
		<li>@item.Name -- @item.Value <a href="#" onclick="edit(@item.Id); return false;">Edit</a></li>
	}
</ul>

@using (Html.BeginForm("EditDefault", "Manage", FormMethod.Post))
{	
	@Html.HiddenFor(m => m.Id)
	<div>
		@Html.LabelFor(m => m.Name)
		@Html.TextBoxFor(m => m.Name)
	</div>
	<div>
		@Html.LabelFor(m => m.Value)
		@Html.TextBoxFor(m => m.Value)
	</div>
	<button id="btnSave">Submit</button>
}

<script type="text/javascript">
	var model;

	$(function () {
		model = $.parseJSON($("#model").val());
		$("#btnSave").click(function () {
			if ($("#Name").val() == "") {
				if ($("#Value").val() != "") {
					alert("Name is required.");
					return false;
				}
			} else if ($("#Value").val() == "") {
				alert("Value is required.");
				return false;
			}
			return true;
		});
	});

	function edit(id) {
		var def = Enumerable.From(model.Defaults).Where(function (item) { return item.Id == id; }).Select(function (item) { return item; }).First();
		$("#Id").val(def.Id);
		$("#Name").val(def.Name);
		$("#Value").val(def.Value);
	}
</script>